#include <STC15F2K60S2.H>
#include "Tim.h"
#include "Seg.h"
#include "Key.h"
#include "Ptim.h"
#include "stdio.h"
#include "Ds1302.h"
#include "iic.h"
#include "LED.h"
unsigned char pucSeg_Buf[9],pucSeg_Code[8],ucSeg_Pos;
unsigned long ulms;
unsigned int ucSeg_Dly;
unsigned char ucKey_Dly,ucKey_Old;
unsigned char ucState;
int ucCount,ucCount_Max,ucCount_Num=0,ucCount_Snum=2000;
unsigned char pucRtc[3]={13,3,5};
unsigned char pucRtcMax[3];
unsigned char ucNum_Flag,ucDelay_Flag,ucCount_Flag;
unsigned char ucpcf_U;
unsigned char ucLed,ucLed_Flag,ucLed_Dly;
void Key_Proc(void);
void Seg_Proc(void);
void Set_Proc(unsigned char ucKey_Down);
void Led_Proc(void);
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	Timer0Init();
	Set_RTC(pucRtc);
	while(1){
		Key_Proc();
		Seg_Proc();
		Led_Proc();
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	 if(!(ulms % 1000)) 
	 { 
			 ucCount=(TH0<<8)+TL0;
			 TH0=0;
			 TL0=0;
			ucCount+=ucCount_Num;
			if(ucCount<0){
				ucCount_Flag=1;
			}
			else{
				ucCount_Flag=0;
			}
	} 
	 if(++ucLed_Dly==200){
		 ucLed_Dly=0;
		 ucLed_Flag^=1;
	 }
	 Led_Disp(ucLed);
	 if(++ucKey_Dly==10) ucKey_Dly=0;
	if(++ucSeg_Dly==100) ucSeg_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
}
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
	 switch(ucKey_Down){
		 case 4:
			 if(++ucState==4) ucState=0;
			if(ucState==0){
				ucNum_Flag=0;
			}
			if(ucState==2){
				ucDelay_Flag=0;
			}
			 break;
		 case 5:
			 if(ucState==1){
				 ucNum_Flag^=1;
			 }
			 if(ucState==3){
				 ucDelay_Flag^=1;
			 }
			 break;
		 case 8:
			 Set_Proc(ucKey_Down);
			 break;
		 case 9:
			 Set_Proc(ucKey_Down);
			 break;
	 }
}
void Seg_Proc(void){
	unsigned char i;
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	if(!ucCount_Flag){
		if(ucCount<=500){
			ucpcf_U=1*51;
		}
		else if(ucCount>500&&ucCount<=ucCount_Snum){
			ucpcf_U=(ucCount/(ucCount_Snum-500)*4)*51;
		}
		else if(ucCount>ucCount_Snum){
			ucpcf_U=5*51;
		}
		PCF8591_Dac(ucpcf_U);
	}
	else {
		ucpcf_U=0;
		PCF8591_Dac(ucpcf_U);
	}
	if(ucCount>ucCount_Max){
		ucCount_Max=ucCount;
		for(i=0;i<3;i++){
			pucRtcMax[i]=pucRtc[i];
		}
	}
	Read_RTC(pucRtc);
	switch(ucState){
		case 0:
			if(!ucCount_Flag){
				sprintf(pucSeg_Buf,"F  %5d",ucCount);
			}
			else{
				sprintf(pucSeg_Buf,"F     LL");
			}
		break;
		case 1:
			if(ucNum_Flag){
				sprintf(pucSeg_Buf,"P2  %4d",ucCount_Num);
			}
			else{
				sprintf(pucSeg_Buf,"P1  %4d",ucCount_Snum);
			}
			break;
		case 2:
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
			break;
		case 3:
			if(ucDelay_Flag){
				sprintf(pucSeg_Buf,"HA%02d%02d%02d",(unsigned int)pucRtcMax[0],(unsigned int)pucRtcMax[1],(unsigned int)pucRtcMax[2]);
			}
			else{
				sprintf(pucSeg_Buf,"HF %5d",ucCount_Max);
			}
			break;
	}
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
void Set_Proc(unsigned char ucKey_Down){
	if(ucState==1){
		if(!ucNum_Flag){
			if(ucKey_Down==8){
			ucCount_Snum+=1000;
			if(ucCount_Snum==10000){
				ucCount_Snum=1000;
			}
		}
			else if(ucKey_Down==9){
				ucCount_Snum-=1000;
				if(ucCount_Snum==0){
					ucCount_Snum=9000;
				}
			}
		}
		else {
			if(ucKey_Down==8){
				ucCount_Num+=100;
				if(ucCount_Num==1000){
					ucCount_Num=-900;
				}
			}
			else{
				ucCount_Num-=100;
				if(ucCount_Num==-1000){
				ucCount_Num=900;
			}
			}
		}
	}
}
void Led_Proc(void){
	if(ucState==0){
		if(ucLed_Flag){
			ucLed|=1;
		}
		else{
			ucLed&=~1;
		}
	}
	else{
		ucLed&=~1;
	}
	if(!ucCount_Flag){
		if(ucCount>ucCount_Snum){
		 if(ucLed_Flag){
			ucLed|=2;
		}
		else{
			ucLed&=~2;
		}
	 }
	 else{
		 ucLed&=~2;
	 }
	}
	else{
		ucLed|=2;
	}
}